import configparser
from src.Data.product_db import ProductDB
from src.Data.user_log import UserLogger

class RecommendationService:
    def __init__(self):
        config = configparser.ConfigParser()
        config.read('src/config/db_config.ini')
        db_config = {
            'host': config['DATABASE']['host'],
            'port': int(config['DATABASE']['port']),
            'user': config['DATABASE']['user'],
            'password': config['DATABASE']['password'],
            'database': config['DATABASE']['database'],
            'product_table': config['DATABASE']['product_table'],
            'alignment_table': config['DATABASE']['alignment_table']
        }
        self.db = ProductDB(db_config)
        self.logger = UserLogger()

    def recommend(self, user_id: str) -> list:
        """为用户推荐商品"""
        # 基于用户行为日志的推荐
        logs = self.logger.query_logs(user_id=user_id)
        # TODO: 实现推荐算法
        return self.db.get_recommended_products()

    def get_random_toys(self, count: int) -> list:
        """随机获取指定数量的玩具商品"""
        query = f"SELECT * FROM {self.db.db_config['product_table']} WHERE category = 'toy' ORDER BY RAND() LIMIT %s"
        cursor = self.db.connection.cursor(dictionary=True)
        cursor.execute(query, (count,))
        return cursor.fetchall()
